<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\{
    Nav as NavModel,
    NavMenu
};
class Nav extends Common{
    //导航模型
    protected $nav;
    //导航菜单模型
    protected $nav_menu;
  public function __construct(Request $request = null)
{
    parent::__construct($request);
    $this->nav = new NavModel();
    $this->nav_menu = new NavMenu();
}
 /**
  *添加或修改导航
  * return json
  */
 public function addNav(){
     if(empty($this->adminInfo)){
         $msg['message']=checkMsg(LOGIN_ERROR);
         return $msg;
     }
     $nav_name = $this->request->post('nav_name');
     $remark   = $this->request->post('remark');
     $nav_id   = $this->request->post('nav_id');
     if(empty($nav_name)){
         $msg['message']=checkMsg(CANSHU_ERROR);
         return $msg;
     }
     $count = $this->nav->where(['name'=>$nav_name])->count();
     if($count>0){
         $msg['message']=checkMsg(NAV_IS_EXIST);
         return $msg;
     }
     if(empty($nav_id)){  //添加
         $data=[
             'name'  =>$nav_name,
             'remark'=>$remark,
         ];
         $res = $this->nav->insertGetId($data);
     }else{
         $data=[
             'name'  =>$nav_name,
             'remark'=>$remark,
         ];
         $res = $this->nav->save($data,['id'=>$nav_id]);
     }
     if($res){
         $msg['message']=checkMsg(OPERA_SUCCESS);
     }else{
         $msg['message']=checkMsg(OPERA_FAIL);
     }
     return $msg;
 }
  /**
   *导航列表
   * return json
   */
  public function navList(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $list = $this->nav->select();
      if(!empty($list)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['list']=$list;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
  /**
   *导航信息
   * return json
   */
  public function navInfo(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $nav_id = $this->request->post('nav_id');
      if(empty($nav_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $info = $this->nav->where(['id'=>$nav_id])->find();
      if(!empty($info)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['info']=$info;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
  /**
   *获取指定导航下所有的菜单
   * return json
   */
 public function getNavMenuList(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $nav_id = $this->request->post('nav_id');
      if(empty($nav_id)){
           $msg['message']=checkMsg(CANSHU_ERROR);
           return $msg;
      }
      $count = $this->nav->where(['id'=>$nav_id])->count();
      if($count<1){
          $msg['message']=checkMsg(NAV_NOT_IS_EXITS);
      }
      $list =$this->nav_menu->where(['nav_id'=>$nav_id])->field("id,name,nav_id,parent_id,path")->order('path')->select();
      $list =$this->nav_menu->cateSort($list);
      if(empty($list)){
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }else{
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['list']=$list;
      }
      return $msg;
  }
   /**
    *删除导航
    * return json
    */
  public function deleteNav(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $nav_id = $this->request->post('nav_id');
      if(empty($nav_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $count = $this->nav_menu->where(['nav_id'=>$nav_id])->count();
      if($count>0){
          $msg['message']=checkMsg(NAV_HAS_MENU);
          return $msg;
      }
     $res = $this->nav->where(['id'=>$nav_id])->delete();
      if($res){
          $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(DELETE_DATA_FAIL);
      }
      return $msg;
  }
  /**
   *添加导航菜单
   * return json
   */
  public function addNavMenu(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $nav_id    = $this->request->post('nav_id');
      $parent_id = $this->request->post('parent_id',0);
      $menu_name = $this->request->post('menu_name');
      $status    = $this->request->post('status',1);
      $icon      = $this->request->post('icon');
      $href      = $this->request->post('href','/');
      $content   = $this->request->post('content');
      if(empty($menu_name) || empty($nav_id)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
      }
      $count = $this->nav_menu->where(['name'=>$menu_name])->count();
      if($count>0){
          $msg['message']=checkMsg(MENU_IS_EXIST);
          return $msg;
      }
      $data=[
          'nav_id'  =>$nav_id,
          'parent_id'=>$parent_id,
          'status'   =>$status,
          'name'     =>$menu_name,
          'href'     =>$href,
          'icon'     =>$icon,
          'text'     =>$content
      ];
      $menu_id = $this->nav_menu->insertGetId($data);
      if($menu_id){
          if($parent_id==0){
              $path="0-".$menu_id;
          }else{
              $parentInfo = $this->nav_menu->where(['id'=>$parent_id])->find();
              $path=$parentInfo['path'].'-'.$menu_id;
          }
         $this->nav_menu->save(['path'=>$path],['id'=>$menu_id]);
          $msg['message']=checkMsg(ADD_SUCCESS);
      }else{
          $msg['message']=checkMsg(ADD_FAIL);
      }
      return $msg;
  }
  /**
   *导航菜单信息
   *return json
   */
  public function getMenuInfo(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $menu_id = $this->request->post('menu_id');
      if(empty($menu_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $info = $this->nav_menu->where(['id'=>$menu_id])->find();
      if(!empty($info)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['info']=$info;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
  /**
   *修改导航栏菜单
   * return json
   */
  public function editeNavMenu(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $menu_id = $this->request->post('menu_id');
      $nav_id    = $this->request->post('nav_id');
      $parent_id = $this->request->post('parent_id',0);
      $menu_name = $this->request->post('menu_name');
      $status    = $this->request->post('status',1);
      $icon      = $this->request->post('icon');
      $href      = $this->request->post('href','/');
      $content   = $this->request->post('content');
      if(empty($menu_name) || empty($nav_id) || empty($menu_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $count = $this->nav_menu->where(['name'=>$menu_name])->count();
      if($count>0){
          $msg['message']=checkMsg(MENU_IS_EXIST);
          return $msg;
      }
      $data=[
          'nav_id'  =>$nav_id,
          'parent_id'=>$parent_id,
          'status'   =>$status,
          'name'     =>$menu_name,
          'href'     =>$href,
          'icon'     =>$icon,
          'text'     =>$content
      ];
      $res = $this->nav_menu->save($data,['id'=>$menu_id]);
      if($res){
          if($parent_id==0){
              $path="0-".$menu_id;
          }else{
              $parentInfo = $this->nav_menu->where(['id'=>$parent_id])->find();
              $path=$parentInfo['path'].'-'.$menu_id;
          }
          $this->nav_menu->save(['path'=>$path],['id'=>$menu_id]);
          $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(UPDATE_DATA_FAIL);
      }
      return $msg;
  }
 /**
  *删除导航菜单
  * return json
  */
public function deleteMenu(){
    if(empty($this->adminInfo)){
        $msg['message']=checkMsg(LOGIN_ERROR);
        return $msg;
    }
    $menu_id = $this->request->post('menu_id');
    if(empty($menu_id)){
        $msg['message']=checkMsg(CANSHU_ERROR);
        return $msg;
    }
    $count = $this->nav_menu->where(['parent_id'=>$menu_id])->count();
    if($count>0){
        $msg['message']=checkMsg(MENU_HAS_CHILD);
        return $msg;
    }
    $res = $this->nav_menu->where(['id'=>$menu_id])->delete();
    if($res){
        $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
    }else{
        $msg['message']=checkMsg(DELETE_DATA_FAIL);
    }
    return $msg;
 }
}